function [a,P] = PassOrWait(P,S,mode)
    global num_players_r;
    global num_players_b;

    if(P{2} == 'r')
        num_players = num_players_r;
    else
        num_players = num_players_b;
    end

    B = S{1};
    B_X = B{1}(1);
    B_Y = B{1}(2);
    
    P_X = P{1}(1);
    P_Y = P{1}(2);
    P_num = P{3};
    
    if(P_num == num_players - 1)
        k = num_players;
    else
        k = mod(P_num + 1, num_players);
    end
    
    if(PlayerInPossession(P,S,mode))
        [a,P] = PassBall(P,S,mode,k);
        SaveMemory(P,true,1+k,0);
    elseif (~TeamInPossession(P,S,mode)) % if this player is closer to theball, go for it
        
        if(IsClosest(P,S,mode))
            [a, P] = GoToBall(P,S,mode);
            SaveMemory(P,true,3+num_players,0);
        else
            %a = '';
            [a,P] = GetOpen(P,S,mode);
        end
    else
        %a = '';
        [a,P] = GetOpen(P,S,mode);
    end
end